package jwebtest.BPC;

import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.NodeClassFilter;
import org.htmlparser.tags.LinkTag;
import org.htmlparser.tags.TableColumn;
import org.htmlparser.tags.TableRow;
import org.htmlparser.tags.TableTag;
import org.htmlparser.util.NodeList;

import com.alibaba.fastjson.JSON;

public class BPC {

	public static void main(String[] args) {
		new BPC().doAction();
	}

	private String cookie = "_gscu_1042262807=41074416o0t0t410; _gscs_1042262807=410744162hy2ni10|pv:10; _gscbrs_1042262807=1; wzwsconfirm=b8d302b1d99810378c0c7ee7adef1b01; wzwstemplate=MQ==; ccpassport=235c44df64befaf3b565d7c1b8ac01b6; wzwschallenge=-1";

	public void doAction() {
		try {
			List<BpcPay> listBpc = new ArrayList<BpcPay>();
			for (int x = 1; x <= 14; x++) {
				String html = BpcUtil.sendPost("http://www.pbc.gov.cn/zhengwugongkai/127924/128041/128120/1923602/1923625/1923629/d6d180ae/index" + x + ".html", cookie);
				BpcUtil.log(html);
				Parser parser = new Parser();
				parser.setInputHTML(html);
				parser.setEncoding("UTF-8");

				NodeFilter filter = new NodeClassFilter(TableTag.class);
				NodeList nodeList = parser.parse(filter);
				int lengs = nodeList.size() >= 35 ? 35 : nodeList.size();
				for (int i = 16; i <= lengs; ++i) {
					System.err.print("【" + i + "】");
					if (nodeList.elementAt(i) instanceof TableTag) {
						TableTag tag = (TableTag) nodeList.elementAt(i);
						TableRow[] rows = tag.getRows();
						for (int j = 0; j < rows.length; ++j) {
							TableRow row = (TableRow) rows[j];
							TableColumn[] columns = row.getColumns();
							String license_number = "", link_url = "", company_name = "", create_time = "";
							for (int k = 0; k < columns.length; ++k) {
								if (k == 1)
									license_number = columns[k].toPlainTextString().trim();
								columns[k].toPlainTextString().trim();
								if (k == 2)
									link_url = ((LinkTag) columns[k].getChild(1)).getAttribute("href");
								if (k == 2)
									company_name = columns[k].toPlainTextString().trim();
								columns[k].toPlainTextString().trim();
								if (k == 3)
									create_time = columns[k].toPlainTextString().trim();
								columns[k].toPlainTextString().trim();
							}// end for k
							BpcPay bpcen = new BpcPay(license_number, company_name, create_time, link_url);
							System.out.println(JSON.toJSON(bpcen));
							listBpc.add(bpcen);
						}// end for j
					}
				}
			}
			FileWriter fw = new FileWriter("E:\\支付机构.txt");
			fw.write(JSON.toJSONString(listBpc) + "");
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
